package com.woniuxy.threeprojcet.end_water_project_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.EmployeeMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.UserMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.Employee;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.PaymentForm;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.PaymentFormMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.User;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.PaymentFormForm;
import com.woniuxy.threeprojcet.end_water_project_admin.service.IPaymentFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 缴费表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-26
 */
@Service
@Slf4j
public class PaymentFormServiceImpl extends ServiceImpl<PaymentFormMapper, PaymentForm> implements IPaymentFormService {
@Resource
private PaymentFormMapper paymentFormMapper;
@Resource
private EmployeeMapper employeeMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    //获得缴费明细
    public List getPayment() {
        List<PaymentForm> paymentForms = paymentFormMapper.selectList(new QueryWrapper());
        log.info("getPayment{}",paymentForms);
        //根据返回的明细里的员工id来查找员工姓名
        ArrayList<PaymentFormForm> paymentFormForms = new ArrayList<>();

        for(int i = 0; i < paymentForms.size(); i++) {
            //得到员工ID
            Integer employeeId = paymentForms.get(i).getEmployeeId();
            System.out.println("得到员工ID:"+employeeId);
            String employeeName = employeeMapper.selectById(employeeId).getEmployeeName();

//            System.out.println("得到员工姓名"+employeeName);

            //得到user对象
            User user = userMapper.selectById(paymentForms.get(i).getUserId());
            System.out.println("得到user对象"+user);

            //创建一个from新对象，有员工姓名属性,user卡号
            PaymentFormForm paymentFormForm = new PaymentFormForm();
//            复制属性
            BeanUtils.copyProperties(paymentForms.get(i),paymentFormForm);
//            设置员工姓名
            paymentFormForm.setEmployeeName(employeeName);
//            设置用户卡号
            paymentFormForm.setCard(user.getCard());

//            设置用户名
            paymentFormForm.setUserName(user.getUserName());
            System.out.println(paymentFormForm);
            //存入新集合
            paymentFormForms.add(paymentFormForm);
        }
        return paymentFormForms;
    }
}
